/*
 * MIT License
 *
 * Copyright (c) 2020 Mr.css
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 */

package cn.seaboot.postman.request.analyzer;

import cn.seaboot.common.core.CommonUtils;
import cn.seaboot.postman.analyzer.MethodInfo;
import cn.seaboot.postman.request.Body;
import cn.seaboot.postman.request.Formdata;
import cn.seaboot.postman.request.Query;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.lang.reflect.Parameter;
import java.util.ArrayList;
import java.util.List;

/**
 * {@link Body}, if request-parameter contain files, you should convert query to formdata.
 *
 * @author Mr.css
 * @date 2021-01-31 14:40
 */
public class DefaultQueryToFormDataConverter implements QueryToFormDataConverter {

    /**
     * If method's parameter contain a MultipartFile, all parameter will convert to Body.
     *
     * @param queries query list
     * @return FormData
     */
    @Override
    public Body convertToFormData(MethodInfo method, List<Query> queries) {
        Body body = new Body();
        List<Formdata> list = new ArrayList<>();
        if (method.getParameters().length > 0) {
            for (Parameter parameter : method.getParameters()) {
                if (MultipartFile.class.equals(parameter.getType())) {
                    list.add(this.getFileFormData(parameter));
                    break;
                }
            }
        }
        if (CommonUtils.isNotEmpty(queries)) {
            for (Query query : queries) {
                Formdata formdata = new Formdata();
                formdata.setKey(query.getKey());
                formdata.setType("text");
                formdata.setDescription(query.getDescription());
                formdata.setValue(query.getValue());
                list.add(formdata);
            }
        }
        body.setMode("formdata");
        body.setFormdata(list.toArray(new Formdata[]{}));
        return body;
    }

    /**
     * Analyze a parameter which is MultipartFile.
     *
     * @param parameter a parameter
     * @return Formdata
     */
    private Formdata getFileFormData(Parameter parameter) {
        RequestParam requestParam = parameter.getAnnotation(RequestParam.class);
        String name, desc;
        if (requestParam == null) {
            desc = "upload file";
            name = parameter.getName();
        } else {
            boolean required = requestParam.required();
            desc = required ? "required | upload file" : "upload file";
            name = requestParam.name();
            if (name.isEmpty()) {
                name = parameter.getName();
            }
        }
        Formdata formdata = new Formdata();
        formdata.setKey(name);
        formdata.setType("file");
        formdata.setDescription(desc);
        return formdata;
    }
}
